﻿@page "/issues"
@inject IMainPlanService MainPlanService

<PageTitle>异常列表</PageTitle>
<!--sticky固定位置，top: 3.5rem-->
<div style="position: sticky; top: 3.5rem; z-index: 1;background-color:White">
    <div class="row">
        <div class="col input-group mb-2">
            <span class="input-group-text">搜索</span>
            <input class="form-control" placeholder="Search"
                   @bind-value="@_search" @oninput="@Search" />
        </div>
        <div class="col form-check ms-3 mt-2" style="max-width: 70px">
            <input class="form-check-input"
                   type="checkbox"
                   value="@_annual"
                   @onchange="OnAnnualChange">
            <label class="form-check-label">
                全年
            </label>
        </div>
        <div class="col input-group mb-2" style="max-width: 230px">
            <span class="input-group-text">月份</span>
            <input class="form-control"
                   type="month"
                   value="@(_selectedMonth.ToString("yyyy-MM"))"
                   format="" @onchange="OnMonthChange" />
        </div>
        <div class="col form-check ms-3 mt-2" style="max-width: 100px">
            <input class="form-check-input" type="checkbox" value="@_fromMonth" @onchange="OnFromMonthChange">
            <label class="form-check-label">
                本月往后
            </label>
        </div>
        <div class="col mt-2">
            <div class="form-check ms-3">
                <input class="form-check-input" type="checkbox" value="@_onlyClosed"
                       @onchange="OnOnlyClosedChange">
                <label class="form-check-label ms-2">Only Closed</label>
            </div>
        </div>
    </div>
</div>
@foreach (var mainPlanDto in _filterMainPlanDtos)
{
    <hr size="1" style="border-top:4px dodgerblue solid;">
    <IssuesMainPlan MainPlanId="@mainPlanDto.Id.ToString()"/>
}

@code {
    //过滤参数
    private string _search = string.Empty;
    private bool _annual;
    private bool _fromMonth = false;
    private DateTime _selectedMonth = DateTime.Today;
    private int _year;
    private int _month;
    private List<MainPlanDto> _mainPlanDtos = new();
    private List<MainPlanDto> _filterMainPlanDtos = new();

    private bool _onlyClosed = false;
    protected override async Task OnParametersSetAsync()
    {
        _year= _selectedMonth.Year;
        _month= _selectedMonth.Month;
        
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnAnnualChange(ChangeEventArgs e)
    {
        _annual =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnFromMonthChange(ChangeEventArgs e)
    {
        _fromMonth =Convert.ToBoolean(e.Value);
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    private async void OnMonthChange(ChangeEventArgs e)
    {
        _selectedMonth = Convert.ToDateTime(e.Value);
        _year= _selectedMonth.Year;
        _month= _selectedMonth.Month;
        await GetData();
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }
    //只显示已关闭的异常
    private void OnOnlyClosedChange(ChangeEventArgs e)
    {
        _onlyClosed = Convert.ToBoolean(e.Value);
        Search(new ChangeEventArgs { Value = _search });
        StateHasChanged();
    }

    private async Task GetData()
    {
        _mainPlanDtos.Clear();
        _mainPlanDtos.AddRange(await MainPlanService.GetBlazorMainPlansAsync(_year, _month, _annual, _fromMonth,true,true));
        //异步方法需要主动告知前台状态已经发生改变
        StateHasChanged();
    }

    private void Search(ChangeEventArgs e)
    {
    //将搜索放在外面做（4.没搜索||5.搜索项目号||6.搜索项目名称）
        _search=e.Value!.ToString()!;
        _filterMainPlanDtos.Clear();
        _filterMainPlanDtos.AddRange(_mainPlanDtos.Where(
            x => x.IssueDtos.Any() &&
                 (!_onlyClosed || x.AllIssueClosed) &&
                (string.IsNullOrEmpty(_search) ||
                 x.Number.ToLower().Contains(_search.ToLower()) ||
                 x.Name.ToLower().Contains(_search.ToLower()))));

        StateHasChanged();
    }
}
